package com.ruoyi.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.course.domain.TCourse;
import com.ruoyi.course.mapper.TCourseMapper;
import com.ruoyi.teacher.domain.THomework;
import com.ruoyi.teacher.domain.TPaper;
import com.ruoyi.teacher.mapper.TPaperMapper;
import com.ruoyi.teacher.vo.THomeworkVo;
import com.ruoyi.teacher.mapper.THomeworkMapper;
import com.ruoyi.teacher.service.ITHomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 作业Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-12
 */
@Service
public class THomeworkServiceImpl extends ServiceImpl<THomeworkMapper, THomework> implements ITHomeworkService {

    @Autowired
    TPaperMapper paperMapper;

    @Autowired
    TCourseMapper courseMapper;

    @Override
    public TableDataInfo<THomeworkVo> queryList(THomeworkVo entity) {
        Page<THomeworkVo> tHomeworkVo = this.baseMapper.queryList(PageUtils.buildPage(), entity);
        List<THomeworkVo> records = tHomeworkVo.getRecords();
        for (THomeworkVo record : records) {
            String cname = null;
            LambdaQueryWrapper<TCourse> cqw = new LambdaQueryWrapper<>();
            cqw.eq(TCourse::getId, record.getcId());
            TCourse tCourse = courseMapper.selectOne(cqw);
            cname = tCourse.getName();

            String pname = null;
            LambdaQueryWrapper<TPaper> pqw = new LambdaQueryWrapper<>();
            pqw.eq(TPaper::getId,record.getpId());
            TPaper tPaper = paperMapper.selectOne(pqw);
            pname = tPaper.getName();

            record.setCourseName(cname);
            record.setPaperName(pname);
        }
        return PageUtils.buildDataInfo(tHomeworkVo);
    }

    @Override
    public List<THomeworkVo> queryAll(THomeworkVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public THomeworkVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }
}
